package com.greatchn.security_demo.handler;

import com.greatchn.security_demo.exception.ApiException;
import com.greatchn.security_demo.exception.ServiceException;
import com.greatchn.security_demo.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.text.MessageFormat;

/**
 * @Description: 异常处理器
 * @Author: zhengbohang
 * @Date: 2021/10/3 10:32
 */
@Slf4j
@RestControllerAdvice
public class ExceptionsHandler {

    @ExceptionHandler(ApiException.class)
    public Result apiExceptionHandler(ApiException ex) {
        return Result.failure(ex.getCode(), ex.getMsg());
    }

    @ExceptionHandler(ServiceException.class)
    public Result srvExceptionHandler(ServiceException ex) {
        return Result.failure(ex.getCode(), ex.getMsg());
    }

    @ExceptionHandler(Exception.class)
    public Result commonExceptionHandler(Exception ex) {
        // TODO: 2023/2/8
        return Result.failure("fail!");
    }
}
